pub mod camelcard;

use camelcard::*;

#[allow(unused)]
fn part1() {
    let maker = Part1CamelCardMaker::new();
    let contents = std::fs::read_to_string("./day07/input.txt").unwrap();
    let mut camelcards: Vec<CamelCard> = contents
        .lines()
        .map(|line| maker.get_camel_card(line))
        .collect();

    // Sort
    let compare = |card1: &CamelCard, card2: &CamelCard| -> std::cmp::Ordering {
        return maker.compare(card1, card2);
    };
    camelcards.sort_by(compare);
    let camelcards = camelcards;

    let sum = camelcards
        .iter()
        .enumerate()
        .fold(0 as u32, |acc, (i, card)| {
            acc + (i as u32 + 1) * card.bid()
        });

    println!("{sum}");
}

#[allow(unused)]
fn part2() {
    let maker = Part2CamelCardMaker::new();
    let contents = std::fs::read_to_string("./day07/input.txt").unwrap();
    let mut camelcards: Vec<CamelCard> = contents
        .lines()
        .map(|line| maker.get_camel_card(line))
        .collect();

    // Sort
    let compare = move |card1: &CamelCard, card2: &CamelCard| -> std::cmp::Ordering {
        return maker.compare(card1, card2);
    };
    camelcards.sort_by(compare);
    let camelcards = camelcards;

    let sum: u32 = camelcards
        .iter()
        .enumerate()
        .map(|(i, card)| (i as u32 + 1) * card.bid())
        .sum();

    println!("{sum}");
}

fn main() {
    part2();
}
